﻿using LJX.SmartPark.Common.Extensions;
using LJX.SmartPark.Common.ResponseOutput;
using LJX.SmartPark.Domain.SafeManage.PatrolPerson;
using LJX.SmartPark.Domain.SysManage;
using LJX.SmartPark.Service.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LJX.SmartPark.Service.SafeManage
{
    public class PatrolPersonServeice:IPatrolPersonServeice
    {
        private readonly IRepository<PatrolPerson> _patrolPersonService;

        public PatrolPersonServeice(IRepository<PatrolPerson> patrolPersonService)
        {
            _patrolPersonService = patrolPersonService;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<Response<bool>> DeletePatrolPersonAsync(PatrolPerson input)
        {
            var result = await _patrolPersonService.DeleteByIdAsync(input.MapTo<PatrolPerson>());
            return Response<bool>.Ok(result > 0);
        }

        /// <summary>
        /// 获取总条数
        /// </summary>
        /// <returns></returns>
        public int GetCount()
        {
            return _patrolPersonService.GetCount();
        }

        /// <summary>
        /// 显示分页
        /// </summary>
        /// <param name="fy2"></param>
        /// <returns></returns>
        public async Task<Response<List<PatrolPerson>>> GetPatrolPersonAsync(Fy2 fy2)
        {
            var students = await _patrolPersonService.GetAllListAsync();
            var result = students.MapToList<PatrolPerson>();

            var totalCount = result.Count();

            result = result.Skip(((fy2.pageIndex - 1) * fy2.pageSize)).Take(fy2.pageSize).ToList();

            return Response<List<PatrolPerson>>.Ok(result, totalCount);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public Response<PatrolPerson> GetPatrolPersonById(PatrolPerson input)
        {
            var userManage = _patrolPersonService.GetListQuery().FirstOrDefault(m => m.Id == input.Id);
            var result = userManage.MapTo<PatrolPerson>();
            return Response<PatrolPerson>.Ok(result);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<Response<bool>> PostAddPatrolPersonAsync(PatrolPerson input)
        {
            var result = _patrolPersonService.InsertAsyncById(input.MapTo<PatrolPerson>());

            return Response<bool>.Ok(result.Result > 0);
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<Response<bool>> PutUpdPatrolPersonAsync(PatrolPerson input)
        {
            var result = _patrolPersonService.UpdateAsync(input.MapTo<PatrolPerson>());

            return Response<bool>.Ok(result.Result > 0);
        }

        /// <summary>
        /// 查询分页
        /// </summary>
        public class Fy2
        {
            public int pageIndex { get; set; }
            public int pageSize { get; set; }
        }
    }
}
